﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;


namespace frm_medic
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void refresh()
        {
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlCommand sqlCommand2 = new SqlCommand();
                sqlCommand.Connection = con;
                sqlCommand2.Connection = con;
                sqlCommand.CommandText = "SELECT * FROM drug;";
                sqlCommand2.CommandText = "SELECT * FROM drug WHERE drug_id =@drug_id;";
                sqlCommand2.Parameters.AddWithValue("@drug_id", "1001");
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                sqlDataAdapter.SelectCommand = sqlCommand;
                DataTable drug = new DataTable();
                con.Open();
                sqlDataAdapter.Fill(drug);
                this.comboBox1.DataSource = drug;
                this.comboBox1.DisplayMember = "drug_id";
                this.comboBox1.ValueMember = "drug_id";
                SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    this.comboBox1.Text = sqlDataReader["drug_id"].ToString();
                    this.textBox1.Text = sqlDataReader["drug"].ToString();
                    this.comboBox2.Text = sqlDataReader["medic"].ToString();
                    this.textBox2.Text = sqlDataReader["sup"].ToString();
                    //this.comboBox3.Text = sqlDataReader["No"].ToString();
                    this.textBox3.Text = sqlDataReader["price"].ToString();
                    this.dateTimePicker1.Text = sqlDataReader["ef_time"].ToString();
                }
                sqlDataReader.Close();
            }


            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {

            refresh();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr);
            try
            {

                con.Open();
                string sql = "insert into drug(drug_id,drug,sup,medic,price,ef_time)VALUES('" + Convert.ToString(comboBox1.Text) + "','" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "', '" + Convert.ToString(comboBox2.Text) + "', '" + Convert.ToString(textBox3.Text) + "', '" + Convert.ToString(dateTimePicker1.Text) + "' )";
                SqlCommand cmd = new SqlCommand(sql,con);
                cmd.Connection = con;
                
                int result = cmd.ExecuteNonQuery();
                //cmd.ExecuteNonQuery();
                if (result > 0 || result == -1)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {

                con.Close();
                refresh();

            }


        }

        
        private void button3_Click(object sender, EventArgs e)
        {
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr);
            try
            {

                con.Open();
                string sql = "delete from drug where drug_id='" + comboBox1.Text + "'";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Connection = con;
                
                
                int result = cmd.ExecuteNonQuery();
                //cmd.ExecuteNonQuery();
                if (result>0 || result == -1)
                {
                    MessageBox.Show("删除成功");
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {

                con.Close();
                refresh();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            print1 p1 = new print1();
            p1.Show();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            string conStr = "Data Source=DESKTOP-CM2RP4R;Initial Catalog=medic;Integrated Security=SSPI";
            SqlConnection con = new SqlConnection(conStr); 
            try
            {
                SqlCommand sqlCommand = new SqlCommand();
                SqlCommand sqlCommand2 = new SqlCommand();
                sqlCommand.Connection = con;
                sqlCommand2.Connection = con;
                sqlCommand.CommandText = "SELECT * FROM drug;";
                sqlCommand2.CommandText = "SELECT * FROM drug WHERE medic =@medic;";
                sqlCommand2.Parameters.AddWithValue("@medic", "胶囊");
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
                sqlDataAdapter.SelectCommand = sqlCommand;
                DataTable drug = new DataTable();
                con.Open();
                sqlDataAdapter.Fill(drug);
                this.comboBox2.DataSource = drug;
                this.comboBox2.DisplayMember = "medic";
                this.comboBox2.ValueMember = "medic";
                SqlDataReader sqlDataReader = sqlCommand2.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    //this.comboBox1.Text = sqlDataReader["drug_id"].ToString();
                    this.textBox1.Text = sqlDataReader["drug"].ToString();
                    this.comboBox2.Text = sqlDataReader["medic"].ToString();
                    this.textBox2.Text = sqlDataReader["sup"].ToString();
                    //this.comboBox3.Text = sqlDataReader["No"].ToString();
                    this.textBox3.Text = sqlDataReader["price"].ToString();
                    this.dateTimePicker1.Text = sqlDataReader["ef_time"].ToString();
                }
                sqlDataReader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }

        }

        private void reportViewer1_Load(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }
    }
}
